/*
 * Copyright 1999-2010 Alibaba.com All right reserved. This software is the
 * confidential and proprietary information of Alibaba.com ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with Alibaba.com.
 */
package com.alibaba.aone.casestudy.module.screen;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.jboss.remoting.util.TimerUtil;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.aone.casestudy.common.entity.Failure;
import com.alibaba.aone.casestudy.common.entity.FailureConditions;
import com.alibaba.aone.casestudy.common.entity.FailureTime;
import com.alibaba.aone.casestudy.common.utils.MetaUtil;
import com.alibaba.aone.casestudy.common.utils.TimeUtil;
import com.alibaba.aone.casestudy.module.action.FailureListAction;
import com.alibaba.aone.casestudy.service.FailureService;
import com.alibaba.aone.casestudy.service.MetaService;
import com.alibaba.aone.casestudy.service.TestService;
import com.alibaba.citrus.turbine.Context;
import com.alibaba.citrus.turbine.dataresolver.Param;
import com.alibaba.common.lang.StringUtil;

/**
 * 故障列表screen类
 * 
 * @author gaojian 2011-4-25 上午10:15:39
 */
public class FailureList {

	@Autowired
	private TestService testService;
	@Autowired
	private FailureService failureService;
	@Autowired
	private MetaService metaService;

	public void execute(@Param("correlationDepa") String correlationDepa,
			@Param("rpDepartment") String rpDepartment,
			@Param("rpMan") String rpMan, @Param("level") Integer[] level,
			@Param("status") Integer[] statusArray,
			@Param("gmtStart") String start, @Param("gmtEnd") String end,
			@Param(name = "page", defaultValue = "1") Integer page,
			Context context) throws ParseException {
		if(paramsLegal(correlationDepa, rpDepartment, rpMan, statusArray, start, end, page)){

			
			FailureConditions failureConditions = new FailureConditions();

			Date gmtStart = null;
			Date gmtEnd = null;

			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm");
			gmtStart = StringUtils.isEmpty(start) ? null : format.parse(start);
			gmtEnd = StringUtils.isEmpty(end) ? null : format.parse(end);

			if ("任意".equals(correlationDepa)) {
				correlationDepa = null;
			}
			if ("任意".equals(rpDepartment)) {
				rpDepartment = null;
			}
			failureConditions.setCorrelationDepa(correlationDepa);
			failureConditions.setRpMan(rpMan);
			failureConditions.setRpDepartment(rpDepartment);

			failureConditions.setGmtStart(gmtStart);
			failureConditions.setGmtEnd(gmtEnd);

			List<Integer> statuses = new ArrayList<Integer>();

			statuses.addAll(Arrays.asList(statusArray));

			List<Integer> ranks = new ArrayList<Integer>();
			ranks.addAll(Arrays.asList(level));

			failureConditions.setRanks(ranks);
			failureConditions.setStatuses(statuses);
			failureConditions.setCurrentPage(page);
			List<Failure> failures = failureService
					.selectFailure(failureConditions);
			
			

			context.put("query", failureConditions);

			context.put("failures", failures);
			
		
		MetaUtil util = new MetaUtil();
		context.put("metaService", metaService);
		context.put("util", util);

		
	
			}
			
	}

	public boolean paramsLegal(Object... params) {
		for (Object param : params) {
			if (param != null) {
				return true;
			}
		}
		return false;
	}

	
}